package com.app.controller;



import com.app.common.EducationTool;
import com.app.model.CusEducationModel;
import com.app.model.CustomerModel;
import com.app.model.UserModel;
import com.app.service.CusEducationService;

import com.app.service.CustomerService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;


@Controller
@RestController
@Data
public class CusEducationController {


    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    UserService userService;
    @Autowired
    CustomerService customerService;

    @Autowired
    CusEducationService cusEducationService;

    @PostMapping("/groupEducation")
    public Result<Object> groupEducation(@RequestParam("schoolName") String schoolName,
                                         @RequestParam("education") Integer education,
                                         @RequestParam("major") String major,
                                         @RequestParam("startTime") String startTime,
                                         @RequestParam("endTime") String endTime,
                                         @RequestParam(required = false) String experience,
                                         @RequestParam("id") int id,
                                  HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405,"token不存在");
        }
        TokenUtils tokenUtil = new TokenUtils();
        String mobile = tokenUtil.verify(token);
        UserModel user = userService.getUserByMobile(mobile);

        if (user == null) {
            return Result.error(405,"用户不存在");

        }
        CustomerModel cus = customerService.getCustomerById(user.getCId());
        CusEducationModel model = new CusEducationModel();
        model.setSchoolName(schoolName);
        model.setEducation(education);
        model.setMajor(major);
        model.setStartTime(startTime);
        model.setEndTime(endTime);
        model.setExperience(experience);
        model.setCustomerId(cus.getId());
        try {
            if (id > 0) {
                model.setId(id);
                model.setUpTime(CommonHelp.getNowTimestamp());
                cusEducationService.update(model);
            } else {
                model.setCrtTime(CommonHelp.getNowTimestamp());
                cusEducationService.insert(model);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return Result.success(null);
    }
    //删除
    @PostMapping("/delEducation")
    public Result<Object> delWorkExp(int id, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405,"token不存在");

        }
        TokenUtils tokenUtil = new TokenUtils();
        String mobile = tokenUtil.verify(token);
        UserModel user = userService.getUserByMobile(mobile);
        if (user == null) {
            return Result.error(405,"用户不存在");
        }
        if (id > 0) {
            cusEducationService.delete(id);
        } else {
            return Result.error(500,"ID不存在");
        }
        return Result.success(null);
    }

    //根据简历ID查询教育经历
    @GetMapping("/getEducationList")
    public Object getWorkExpList(HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        String id = request.getParameter("customerId");

        CustomerModel cus = null;
        if (id != null && !"0".equals(id) && !"undefined".equals(id)) {
            cus = customerService.getCustomerById(Integer.valueOf(id));
        } else {
            if (token == null) {
                return Result.error(405,"token不存在");
            }
            TokenUtils tokenUtil = new TokenUtils();
            String mobile = tokenUtil.verify(token);
            UserModel user = userService.getUserByMobile(mobile);
            if (user == null) {
                return Result.error(405,"用户不存在");
            }
            cus = customerService.getCustomerById(user.getCId());
        }
        List<CusEducationModel> educationList = cusEducationService.getWorkExpList(cus.getId());
        if (educationList != null && educationList.size() > 0)//学历
        {

            for (int i = 0; i < educationList.size(); i++) {
                for (int k = 0; k < EducationTool.EDUCATION_VO_LIST.size(); k++) {
                    if (educationList.get(i).getEducation() == EducationTool.EDUCATION_VO_LIST.get(k).getId()) {
                        educationList.get(i).setEducationText(EducationTool.EDUCATION_VO_LIST.get(k).getData());
                        break;
                    }
                }
                if (educationList.get(i).getEducation() == null) {
                    educationList.get(i).setEducationText("暂无学历");
                }

            }
        }
        return Result.success(educationList);

    }

    //根据ID查询教育经历
    @GetMapping("/getCusEducation")
    public Object getWorkExp(HttpServletRequest request) throws ParseException {
        return Result.success(cusEducationService.getWorkExp(request));

    }
}
